package org.ruanjj.baseknowleadgedemo;

import java.io.*;

public class SerialObejectDemo {
    public static void main(String[] args) throws FileNotFoundException {
        //将对象实例化后序列化到文件
        try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                new FileOutputStream("employee.bat")
        )){
            Employee employee = new Employee("test",20);
            objectOutputStream.writeObject(employee);
            System.out.println("序列化完成");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        //再从文件中反序列化到对象
        try(ObjectInputStream inputStream = new ObjectInputStream(
                new FileInputStream("employee.bat")
        )){
            Employee employee = (Employee) inputStream.readObject();
            System.out.println("反序列化完成");
            System.out.println(employee.getName());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
}
